package com.sharp.kernel.config.db.key;

import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.kernel.mybatis.dao.mapper.SequenceDao;
import com.sharp.kernel.utils.BeanUtil;
import com.sharp.kernel.validate.Validator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.genid.GenId;

/**
 * Title: SequenceGenId
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2021/8/20 15:06
 */
@Component
public class SequenceGenId implements GenId<String> {

    @Override
    public String genId(String table, String column) {
        String id = BeanUtil.getBean(SequenceDao.class).getSeq(buildSeqName(table, column));
        Validator.assertNotBlank(id, ErrCodeSys.SYS_CONFIG_NOT_EXIST, String.format("表%s-列%s,序列生成", table, column));
        return id;
    }

    private String buildSeqName(String table, String column) {
        return StringUtils.capitalize(table) + "_" + StringUtils.capitalize(column) + "_SEQ";
    }
}
